package cn.gdut.chengcai.base;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.ui.ModelMap;

import cn.gdut.chengcai.entity.CourseChapterInfo;
import cn.gdut.chengcai.entity.CourseInfo;
import cn.gdut.chengcai.entity.CourseInfoExample;
import cn.gdut.chengcai.entity.CourseLessonInfo;
import cn.gdut.chengcai.enums.CourseCategoryType;
import cn.gdut.chengcai.enums.CourseLevelType;
import cn.gdut.chengcai.param.CourseSearchParam;
import cn.gdut.chengcai.service.CourseInfoService;
import cn.gdut.chengcai.vo.CourseChapterInfoVo;
import cn.gdut.chengcai.vo.CourseInfoVo;
import cn.gdut.chengcai.vo.CourseLessonInfoVo;

public class CourseUtil {
	public static String DEFAULT_LOGO_URL = "";

	public static void resetSearchParamConstant(CourseSearchParam param) {
		// param.setSearchType(SearchType.ONLINE_COURSE.getValue());
		param.setCategoryList("不限-JavaWeb-Android-IOS-PHP-Javascript-ASP");
		// param.setSpeakerNameList("不限-name1-name2-name3-name4");
		param.setGradeList("不限-初级-中级-高级-专家-首席");
		param.setSortOrderList("默认-最新↓-价格↓-最热↓-评论最多↓");
	}

	public static void resetSearchParam(CourseSearchParam param) {
		resetSearchParamConstant(param);
		// 恢复默认值
		param.setPageNo("1");
		param.setCategorySelected("不限");
		param.setGradeSelected("不限");
		param.setSortOrderSelected("默认");
	}

	public static List<CourseInfoVo> filter(HttpSession session, ModelMap map, CourseInfoService courseInfoService,
			CourseSearchParam newParam) throws Exception {
		CourseSearchParam param = null;
		CourseInfoExample example = new CourseInfoExample();
		CourseInfoExample.Criteria criteria = example.createCriteria();

		if (newParam != null) {// 如果新参数不为空，则放到session中
			CourseUtil.resetSearchParamConstant(newParam);
			param = newParam;
		} else {// 从session中获取参数
			CourseSearchParam paramSession = (CourseSearchParam) session
					.getAttribute(ConstantUtil.SESSION_SEARCH_PARAM);
			if (paramSession != null) {
				param = paramSession;
			}
		}

		if (param != null) {
			if (StringUtils.isEmpty(param.getKeyword()) == false) {
				criteria.andNameLike("%" + param.getKeyword() + "%");
				// criteria.andNameEqualTo(param.getKeyword());
			}

			if (StringUtils.isEmpty(param.getCategorySelected()) == false
					&& param.getCategorySelected().equals("不限") == false) {
				criteria.andCatalogIdEqualTo(
						Byte.parseByte(CourseCategoryType.getInteger(param.getCategorySelected()).toString()));
			}

			if (StringUtils.isEmpty(param.getGradeSelected()) == false
					&& param.getGradeSelected().equals("不限") == false) {
				criteria.andLevelEqualTo(
						Byte.parseByte(CourseLevelType.getInteger(param.getGradeSelected()).toString()));
			}
		} else {
			param = new CourseSearchParam();
			resetSearchParam(param);
		}
		criteria.andIsDeletedEqualTo(false);

		String sortString = null;
		if (StringUtils.isEmpty(param.getSortOrderSelected()) == false) {
			if (param.getSortOrderSelected().startsWith("最新")) {
				sortString = "create_time_ desc";
				example.setOrderByClause(sortString);
			} else if (param.getSortOrderSelected().startsWith("价格")) {
				sortString = "total_price_ desc";
				example.setOrderByClause(sortString);
			} else if (param.getSortOrderSelected().startsWith("最热")) {
				sortString = "view_count_ desc";
				example.setOrderByClause(sortString);
			} else if (param.getSortOrderSelected().startsWith("评论最多")) {
				sortString = "comment_count_ desc";
				example.setOrderByClause(sortString);
			}
		}

		List<CourseInfo> listTotal = courseInfoService.selectByExample(example);
		Long itemCount = Long.valueOf(String.valueOf(listTotal.size()));
		Pager pager = PagerUtil.getPager(session, itemCount);
		int fromIndex = (pager.getPageNo() - 1) * pager.getPageSize();
		if (fromIndex >= listTotal.size()) {
			fromIndex = 0;
			pager.setPageNo(1);
			session.setAttribute(ConstantUtil.STR_PAGE_NO, "1");
		}
		int toIndex = fromIndex + pager.getPageSize();
		if (toIndex >= listTotal.size()) {
			toIndex = listTotal.size();
		}
		List<CourseInfo> list = listTotal.subList(fromIndex, toIndex);
		List<CourseInfoVo> voList = new ArrayList<>();
		if (list != null && list.size() > 0) {
			fillCourseVoList(list, voList);
		}

		param.setResultNumber(itemCount);
		session.setAttribute(ConstantUtil.SESSION_SEARCH_PARAM, param);

		if (map != null) {
			map.put("list", voList);
			session.setAttribute("pager", pager);
		}

		return voList;
	}

	public static void fillVo(CourseInfo entity, CourseInfoVo vo) throws Exception {
		if (null != entity) {
			BeanUtils.copyProperties(vo, entity);
			if (null != entity.getIsFree()) {
				if (entity.getIsFree() == true) {
					vo.setIsFree("免费");
				} else {
					vo.setIsFree("收费");
				}
			}

			if (null != entity.getLevel()) {
				vo.setLevel(CourseLevelType.getString((int) entity.getLevel()));
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (null != entity.getCreateTime()) {
				vo.setCreateTime(sdf.format(entity.getCreateTime()));
			}
			if (null != entity.getUpdateTime()) {
				vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
			}
		}

	}

	public static void fillVo(CourseChapterInfo entity, CourseChapterInfoVo vo) throws Exception {
		if (null != entity) {
			BeanUtils.copyProperties(vo, entity);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (null != entity.getCreateTime()) {
				vo.setCreateTime(sdf.format(entity.getCreateTime()));
			}
			if (null != entity.getUpdateTime()) {
				vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
			}
		}

	}

	public static void fillVo(CourseLessonInfo entity, CourseLessonInfoVo vo) throws Exception {
		if (null != entity) {
			BeanUtils.copyProperties(vo, entity);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (null != entity.getCreateTime()) {
				vo.setCreateTime(sdf.format(entity.getCreateTime()));
			}
			if (null != entity.getUpdateTime()) {
				vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
			}
		}

	}

	public static List<CourseInfoVo> fillCourseVoList(List<CourseInfo> list, List<CourseInfoVo> voList)
			throws Exception {

		if ((null != list) && (list.size() > 0)) {
			for (CourseInfo entity : list) {
				CourseInfoVo vo = new CourseInfoVo();
				fillVo(entity, vo);
				voList.add(vo);
			}
		}

		return voList;
	}

	public static List<CourseChapterInfoVo> fillChapterVoList(List<CourseChapterInfo> list,
			List<CourseChapterInfoVo> voList) throws Exception {

		if ((null != list) && (list.size() > 0)) {
			for (CourseChapterInfo entity : list) {
				CourseChapterInfoVo vo = new CourseChapterInfoVo();
				fillVo(entity, vo);
				voList.add(vo);
			}
		}

		return voList;
	}

	public static List<CourseLessonInfoVo> fillLessonVoList(List<CourseLessonInfo> list,
			List<CourseLessonInfoVo> voList) throws Exception {

		if ((null != list) && (list.size() > 0)) {
			for (CourseLessonInfo entity : list) {
				CourseLessonInfoVo vo = new CourseLessonInfoVo();
				fillVo(entity, vo);
				voList.add(vo);
			}
		}

		return voList;
	}

}
